<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from '@/views/components/mixins/resize.js'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ month, orders } = {}) {
      this.chart.setOption({
        // title: {
        //   text: 'World Population'
        // },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          bottom: '0'

        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '15%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01],
          axisLine: {
            lineStyle: {
              color: '#d7d9dd',
              // width: 1, //这里是为了突出显示加上的
            }
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#565759",
              fontSize: 16
            }
          }
        },
        yAxis: {
          type: 'category',
          data: ['马立', 'Indonesia', 'USA', 'India', 'China', 'World'],
          axisLine: {
            lineStyle: {
              color: '#333',
              // width: 1, //这里是为了突出显示加上的
            }
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#333",
              fontSize: 16
            }
          }
        },
        series: [
          {
            name: '最大服务点数',
            type: 'bar',
            data: [18203, 23489, 29034, 104970, 131744, 630230],
            color: "#009dff"
          },
          {
            name: '已使用服务点数',
            type: 'bar',
            data: [19325, 23438, 31000, 121594, 134141, 681807],
            color: "#1ebb45"
          }
        ]
      })
    }
  }
}
</script>
